<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/9/21
 * Time: 15:39
 */

namespace app\www\controller;

use think\Session;
use think\Db;
use app\common\model\User;
use think\Cache;

class Money extends Common
{
    // 钱包首页
    public function index()
    {
        $access_token=Session::get('access_token');
        /*本人信息*/
        $user=User::userFromToken($access_token);

        $password=Session::get('second_password');

        $time=time();
        $data1=array(
            "time"=>$time,
            "username"=>$user['mobile'],
            "code"=>base64_encode(base64_encode($password)),
            "token"=>md5(md5(substr($time,3)))
        );
        $url=config("post_url")."/commerce/getUserKind";
       $res= curl_post($url,json_encode($data1));
       $data=json_decode($res,true);
       if($data['error_code']!=0)
       {
            $this->error("请求失败！");
       }
       $chart=$data['result']['chart'];
       $data_arr=[];
       foreach ($chart as $key=>$value)
       {
           $arr=array(
               "year"=>$key,
               "value"=>floatval($value)
           );
           array_unshift($data_arr,$arr);

       }
        $data['result']['chart']=json_encode($data_arr);
       $this->assign('data',$data['result']);
        return $this->fetch();
    }
    // 密码获取
    public function ajax()
    {
        $password=input('password');
      if(empty($password))
      {
          return 0;
      }else{
        Session::set('second_password',$password);
        return 1;
      }

    }
    /*发送钱包*/
    public function sendMoney()
    {
        $accounts=input('accounts');
        $access_token=Session::get('access_token');
        $code=Session::get('second_password');
        /*本人信息*/
        $user=User::userFromToken($access_token);
        if(request()->isAjax())
        {
            $data=input();
            $time=time();
            $data1=array(
                "time"=>$time,
                "username"=>$user['mobile'],
                "code"=>base64_encode(base64_encode($code)),
                "balance"=>$data['balance'],
                "remark"=>$data['remark'],
                "accounts"=>$data['accounts'],
                "token"=>md5(md5(substr($time,3)))
            );
            $url=config("post_url")."/commerce/transferBalance";
            $res= curl_post($url,json_encode($data1));
            $data2=json_decode($res,true);
            if($data2['error_code']==0)
            {
                $this->ajaxSuccess("转出成功！",url('Money/index'));
            }else{
                $this->ajaxError($data2['result']);
            }
        }else{
            $time=time();
            $data=array(
                "time"=>$time,
                "mobile"=>$user['mobile'],
                "token"=>md5(md5(substr($time,3)))
            );
            $url=config("post_url")."/commerce/usertrue";
            $res= curl_post($url,json_encode($data));
            $data=json_decode($res,true);
            if($data['error_code']==1)
            {
                $data['result']['hlink']=0;
            }

            $this->assign('accounts',$accounts);
            $this->assign('hlink',$data['result']['hlink']);
            return $this->fetch();
        }

    }
}